package com.xckj.cycling.adapter;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.inuker.bluetooth.library.search.SearchResult;
import com.xckj.cycling.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @Author: ChenYao
 * @Describe: 搜索设备
 * @Date: 2023/3/19 23:31
 */
public class SearchDeviceAdapter extends RecyclerView.Adapter<SearchDeviceAdapter.MyViewHolder> implements Comparator<SearchResult> {
    private final List<SearchResult> mDevices;
    private OnItemClickListener onItemClickListener;

    public SearchDeviceAdapter() {
        mDevices = new ArrayList<>();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<SearchResult> devices) {
        mDevices.clear();
        mDevices.addAll(devices);
        Collections.sort(mDevices, this);
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public SearchDeviceAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_device_added, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        SearchResult searchResult = mDevices.get(position);
        if (TextUtils.isEmpty(searchResult.getName()) || "NULL".equals(searchResult.getName())) {
            holder.tvDeviceName.setText(searchResult.getAddress());
        }else {
            holder.tvDeviceName.setText(searchResult.getName());
        }
        holder.tvStatus.setText(R.string.connect);
    }

    @Override
    public int getItemCount() {
        return mDevices.size();
    }

    @Override
    public int compare(SearchResult lhs, SearchResult rhs) {
        return rhs.rssi - lhs.rssi;
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        private final TextView tvDeviceName;
        private final TextView tvStatus;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDeviceName = itemView.findViewById(R.id.tv_device_name);
            tvStatus = itemView.findViewById(R.id.tv_status);
            itemView.setOnClickListener(view -> {
                if (null != onItemClickListener) {
                    SearchResult searchResult = mDevices.get(getAdapterPosition());
                    onItemClickListener.onItemClick(searchResult);
                }
            });
        }
    }

    public interface OnItemClickListener {
        /**
         * Item的点击事件
         *
         * @param searchResult 当前索引
         */
        void onItemClick(SearchResult searchResult);
    }

}

